#include "json/json.h"
#include <string.h>
#include <string>

typedef struct request
{
    int x;
    int y;
    char op;
}request_t;

typedef struct response
{
    int code; //退出信息，0:正常退出，-1:除零错误
    int result; //正常计算的结果
}response_t;

//序列化请求
string serialize_request(const request_t& rq)
{
    Json::Value root;
    root["x"] = rq.x;
    root["y"] = rq.y;
    root["op"] = rq.op;

    Json::FastWriter writer;
    std::string s = writer.write(root);

    return s;
}

//序列化响应
string serialize_response(const response_t& resp)
{
    Json::Value root;
    root["code"] = resp.code;
    root["result"] = resp.result;

    Json::FastWriter writer;
    std::string s = writer.write(root);

    return s;
}

//反序列化请求
void deserialize_request(const std::string& rq, request_t& out)
{
    Json::Value root;
    Json::Reader reader;
    reader.parse(rq, root);

    out.x = root["x"].asInt();
    out.y = root["y"].asInt();
    out.op = root["op"].asInt();
}

//反序列化响应
void deserialize_response(const std::string& resp, response_t& out)
{
    Json::Value root;
    Json::Reader reader;
    reader.parse(resp, root);

    out.code = root["code"].asInt();
    out.result = root["result"].asInt();
}